package com.exam.demo.service.impl;

import com.exam.demo.mapper.TeacherMapper;
import com.exam.demo.pojo.Student;
import com.exam.demo.pojo.Teacher;
import com.exam.demo.pojo.TeacherExample;
import com.exam.demo.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class TeacherImpl implements TeacherService {
    @Autowired
    private TeacherMapper teacherMapper;

    @Override
    @Transactional
    //注册方法,成功返回1，否则返回0
    public int add(Teacher teacher) {
        return this.teacherMapper.insert(teacher);
    }

    @Override
    @Transactional
    //登录方法，返回登录是否成功
    public Boolean login(String teaNo, String teaPwd) {
        Teacher teacher = this.teacherMapper.selectByPrimaryKey(teaNo);
        if(teacher.getTeaPwd().equals(teaPwd))
            return true;
        else return false;
    }

    @Override
    public Teacher getInfo(String teaNo) {
        Teacher teacher = this.teacherMapper.selectByPrimaryKey(teaNo);
        teacher.setTeaPwd(null);
        return teacher;
    }

    @Override
    @Transactional
    //修改个人资料,只更新提交的字段,成功返回1，否则返回0
    public int updateTeaInfo(Teacher teacher) {
        return this.teacherMapper.updateByPrimaryKeySelective(teacher);
    }

    @Override
    @Transactional
    //修改密码,虽然和上面那个方法一样，但是分的清楚一点,成功返回1，否则返回0,原密码错误返回2
    public int updateTeaPwd(String teaNo,String oldTeaPwd,String newTeaPwd) {
        Teacher teacher = this.teacherMapper.selectByPrimaryKey(teaNo);
        if(oldTeaPwd.equals(teacher.getTeaPwd())) {
            teacher.setTeaPwd(newTeaPwd);
            return this.teacherMapper.updateByPrimaryKeySelective(teacher);
        }
        else return 2;
    }

    @Override
    public List<Teacher> getTeacherListByTeaNo(String teaNo) {
        TeacherExample teacherExample = new TeacherExample();
        TeacherExample.Criteria criteria = teacherExample.createCriteria();
        criteria.andTeaNoLike(teaNo+"%");
        return this.teacherMapper.selectByExample(teacherExample);
    }

    @Override
    public List<Teacher> getTeacherListByTeaName(String teaName) {
        TeacherExample teacherExample = new TeacherExample();
        TeacherExample.Criteria criteria = teacherExample.createCriteria();
        criteria.andTeaNameEqualTo(teaName);
        return this.teacherMapper.selectByExample(teacherExample);
    }

    @Override
    public List<Teacher> getTeacherListByTeaDiscipline(String teaDiscipline) {
        TeacherExample teacherExample = new TeacherExample();
        TeacherExample.Criteria criteria = teacherExample.createCriteria();
        criteria.andTeaDisciplineEqualTo(teaDiscipline);
        return this.teacherMapper.selectByExample(teacherExample);
    }

    @Override
    public List<Teacher> getTeacherListByTeaMail(String teaMail) {
        TeacherExample teacherExample = new TeacherExample();
        TeacherExample.Criteria criteria = teacherExample.createCriteria();
        criteria.andTeaMailLike(teaMail+"%");
        return this.teacherMapper.selectByExample(teacherExample);
    }


}
